package com.handu.luna.core.domain.promotion;

import com.handu.luna.core.domain.BasePersistentDomain;
import com.handu.luna.core.domain.advertisement.AdPositionId;
import com.handu.luna.core.domain.goods.Goods;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import javax.persistence.Entity;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.Transient;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangzailiang on 2014/7/30.
 */
@Entity
public class TopicModule extends BasePersistentDomain<TopicModuleRepository>{

    public static final String DELIMITER = ",";

    //模块名称
    private String name;
    //模块商品
    private String goods;
    //商品数量
    private int goodsNum;
    //排序
    private int displayOrder = 100;
    //广告位ID
    private long adPositionId;

    @Transient private AdPositionId adPosition;
    @Transient private List<Goods> goodsList;
    @Transient private List<Long> goodsIdList;

    public TopicModule() {
    }

    @PostLoad
    public void init() {
        if(StringUtils.isNotBlank(goods)) {
            goodsList = new ArrayList<>();
            goodsIdList = new ArrayList<>();
            for (String goodsStrId : this.strToList(goods)) {
                long goodsId = Long.parseLong(goodsStrId);
                goodsList.add(context.getGoodsRepository().findOne(goodsId));
                goodsIdList.add(goodsId);
            }
        }

        if(adPositionId != 0) {
            adPosition = context.getAdPositionIdRepository().findOne(adPositionId);
        }
    }

    @PrePersist
    public void prePersist() {

        if(CollectionUtils.isNotEmpty(goodsIdList)) {
            goods = this.listToStr(goodsIdList);
        }else {
            goods = "";
        }
    }

    public TopicModule update() {
        prePersist();
        return context.getTopicModuleRepository().save(this);
    }

    public String listToStr(List<Long> list) {
        StringBuilder sb = new StringBuilder();
        for (Long id : list) {
            sb.append(String.valueOf(id)).append(DELIMITER);
        }
        return sb.toString();
    }

    public List<String> strToList(String arrayString) {
        List<String> list = new ArrayList<String>();
        for(String item : arrayString.split(DELIMITER)) {
            list.add(item);
        }
        return list;
    }


    public List<Goods> getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(List<Goods> goodsList) {
        this.goodsList = goodsList;
    }

    public int getGoodsNum() {
        return goodsNum;
    }

    public void setGoodsNum(int goodsNum) {
        this.goodsNum = goodsNum;
    }

    public int getDisplayOrder() {
        return displayOrder;
    }

    public void setDisplayOrder(int displayOrder) {
        this.displayOrder = displayOrder;
    }

    public String getGoods() {
        return goods;
    }

    public void setGoods(String goods) {
        this.goods = goods;
    }

    public List<Long> getGoodsIdList() {
        return goodsIdList;
    }

    public void setGoodsIdList(List<Long> goodsIdList) {
        this.goodsIdList = goodsIdList;
    }

    public long getAdPositionId() {
        return adPositionId;
    }

    public void setAdPositionId(long adPositionId) {
        this.adPositionId = adPositionId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public AdPositionId getAdPosition() {
        return adPosition;
    }

    public void setAdPosition(AdPositionId adPosition) {
        this.adPosition = adPosition;
    }


    
    
}
